//
//  HomeQuestionCollectionViewCell.m
//  ZJControlsClass
//
//  Created by mac on 2024/11/16.
//

#import "HomeQuestionCollectionViewCell.h"

#import "HomeQuestionHeadView.h"
#import "HomeQuestionChooseTableViewCell.h"

@interface HomeQuestionCollectionViewCell ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UIView * follewView;
@property (nonatomic,strong) HomeQuestionHeadView * headView;
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;
@property (nonatomic,strong) NSMutableArray * chooseData;

@end



@implementation HomeQuestionCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if(self){
    
        [self configUI];
        
    }
    
    return self;
}


-(void)configUI{
    
    [self.contentView addSubview:self.follewView];
    [self.follewView addSubview:self.tableView];
    
    
    [self.follewView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(ALIGN_WIDTH_PT(12));
        make.right.equalTo(self.contentView).with.offset(-ALIGN_WIDTH_PT(12));
        make.top.bottom.equalTo(self.contentView);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.follewView);
    }];
    
    
    
}

-(void)setModel:(HomeQuestionModel *)model{
    _model = model;
    self.dataSource = [NSMutableArray arrayWithArray:model.optionsList];
    self.chooseData = @[].mutableCopy;
    [self.chooseData addObject:model.myAnswer];
    [self.tableView reloadData];
    
}




#pragma amrk -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeQuestionChooseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeQuestionChooseTableViewCell"];
    if (!cell){
        cell = [[HomeQuestionChooseTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeQuestionChooseTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    HomeQuestionOptionsListModel * model = self.dataSource[indexPath.row];
    if (model.select){
        cell.msgLabel.textColor = COLOR_RGB(50, 113, 237);
    }else{
        cell.msgLabel.textColor = [UIColor blackColor];
    }
    
    cell.model = model;
    
    
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HomeQuestionHeadView * headView = [[HomeQuestionHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - ALIGN_WIDTH_PT(60), 95)];
    headView.backgroundColor = [UIColor whiteColor];
    
    if ([self.model.type intValue] == 1){ // 多选
        headView.typeLabel.text = @"多选题";
    }else{ // 0 是单选
        headView.typeLabel.text = @"单选题";
    }
    
    headView.msgLabel.text = [NSString stringWithFormat:@"%@.%@",self.model.sort,self.model.stem];
    
    return headView;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    HomeQuestionOptionsListModel * model = self.dataSource[indexPath.row];
    HomeQuestionChooseTableViewCell * cell = (HomeQuestionChooseTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

    if ([self.model.type intValue] == 1){
        // 多选
        if (model.select){ // 选中
            model.select = NO;
            cell.msgLabel.textColor = COLOR_RGB(50, 113, 237);
            [self.chooseData removeObject:model.title];
        }else{ // 未选
            model.select = YES;
            [self.chooseData addObject:model.title];
        }
        
        self.model.myAnswer = [self.chooseData componentsJoinedByString:@"|"];
        [self.tableView reloadData];
        
    }else{
        // 单选
        [self.dataSource enumerateObjectsUsingBlock:^(HomeQuestionOptionsListModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.select){
                obj.select = NO;
            }
        }];
        model.select = YES;
        self.model.myAnswer = model.title;
        [self.tableView reloadData];
    }
    
    
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(60);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return ALIGN_HEIGHT_PT(95);
}


#pragma mark -- 懒加载
-(UIView *)follewView{
    if (!_follewView){
        _follewView = [UIView new];
        _follewView.layer.cornerRadius = 15;
        _follewView.layer.masksToBounds = YES;
        _follewView.backgroundColor = [UIColor whiteColor];
    }
    return _follewView;
}

-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStyleGrouped)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
    }
    return  _tableView;
}



@end
